/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lector;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

/**
 *
 * @author nexo__000
 */
public class Lector {

    static String ficha;

    public static Document getDOMDoc(String file) {
        Document doc = null;
        try {
            File xmlFile = new File("./src/examples/ejemplo3.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            doc = (Document) dBuilder.parse(xmlFile);
        } catch (Exception ex) {
            Logger.getLogger(Lector.class.getName()).log(Level.SEVERE, null, ex);
        }
        return doc;
    }

    public static void printFicha() {
    }

    public static void selectAndRead(String fich) {
        ficha = fich;
        read("fuerza");
        read("destreza");
        read("inteligencia");
    }

    public static void read(String atributo) {
        SAXBuilder builder = new SAXBuilder();
        File xmlFile = new File("./src/examples/" + ficha);
        try {

            Document document = (Document) builder.build(xmlFile);
            Element rootNode = document.getRootElement();
            List list = rootNode.getChildren();
            System.out.print("\nLa " + atributo + " del personaje es: ");
            int suma = 0;
            int valor = 0;
            for (int i = 0; i < list.size(); i++) {
                Element node = (Element) list.get(i);
                System.out.print(node.getName());
                if (node.getName().equals("raza")) {
                    valor = leerAtributoRaza(atributo, getRaza(node));
                    System.out.print(" (" + getRaza(node) + ") ");
                } else {
                    valor = Integer.parseInt(node.getChildText(atributo));
                }
                System.out.print(": ");
                System.out.print(valor);
                suma += valor;
                if (i < list.size() - 1) {
                    System.out.print(" + ");
                } else {
                    System.out.print(" = " + suma);
                }
            }

        } catch (IOException io) {
            System.out.println(io.getMessage());
        } catch (JDOMException jdomex) {
            System.out.println(jdomex.getMessage());
        }
    }

    public static void leer() {
        leerAtributo("fuerza");
        leerAtributo("destreza");
        leerAtributo("inteligencia");
    }

    public static int leerAtributoRaza(String atributo, String raza) {
        int resultado = 0;
        try {
            SAXBuilder builder = new SAXBuilder();
            File xmlFile = new File("./src/examples/razas.xml");

            Document document = (Document) builder.build(xmlFile);
            Element rootNode = document.getRootElement();
            List list = rootNode.getChildren();
            int posicion = buscarEtiqueta(rootNode, raza);
            if (posicion != -1) {
                Element node = (Element) list.get(posicion);
                resultado = Integer.parseInt(node.getChildText(atributo));
            }
        } catch (JDOMException | IOException ex) {
            Logger.getLogger(Lector.class.getName()).log(Level.SEVERE, null, ex);
        }
        return resultado;
    }

    private static int buscarEtiqueta(Element pajar, String aguja) {
        int posicion = -1;
        List lista = pajar.getChildren();
        for (int i = 0; i < lista.size(); i++) {
            Element node = (Element) lista.get(i);
            if (node.getName().equals(aguja)) {
                posicion = i;
            }
        }
        return posicion;
    }

    public static void leerAtributo(String atributo) {
        SAXBuilder builder = new SAXBuilder();
        File xmlFile = new File("./src/examples/ejemplo2.xml");
        try {
            Document document = (Document) builder.build(xmlFile);
            Element rootNode = document.getRootElement();
            List list = rootNode.getChildren();
            System.out.print("\nLa " + atributo + " del personaje es: ");
            int suma = 0;
            int valor = 0;
            for (int i = 0; i < list.size(); i++) {
                Element node = (Element) list.get(i);
                System.out.print(node.getName());
                if (node.getName().equals("raza")) {
                    valor = leerAtributoRaza(atributo, getRaza(node));
                    System.out.print(" (" + getRaza(node) + ") ");
                } else {
                    valor = Integer.parseInt(node.getChildText(atributo));
                }
                System.out.print(": ");
                System.out.print(valor);
                suma += valor;
                if (i < list.size() - 1) {
                    System.out.print(" + ");
                } else {
                    System.out.print(" = " + suma);
                }
            }
        } catch (IOException io) {
            System.out.println(io.getMessage());
        } catch (JDOMException jdomex) {
            System.out.println(jdomex.getMessage());
        }
    }

    private static String getRaza(Element pajar) {
        List lista = pajar.getChildren();
        Element node = (Element) lista.get(0);
        return node.getName();

    }

    private static Element getHijoInmediato(Element padre) {
        List lista = padre.getChildren();
        return (Element) lista.get(0);
    }
}
